// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * Utf8TextCoder.h
 *
 *  Created on: Dec 15, 2009
 *      Author: dhovemey
 */

#ifndef UTF8TEXTCODER_H_
#define UTF8TEXTCODER_H_

#include "util/TextCoder.h"

class Utf8TextCoder : public TextCoder {
private:
	Utf8TextCoder& operator=(const Utf8TextCoder &);
	Utf8TextCoder(const Utf8TextCoder &);

	bool m_decodeError;
	bool m_encodeError;

public:
	Utf8TextCoder();
	virtual ~Utf8TextCoder();

	virtual long decode(InputStream *in);
	virtual bool encode(wchar_t c, OutputStream *out);
	virtual bool decodeError();
	virtual bool encodeError();

private:
	int nextByte(InputStream *in);
	int getContinuationByte(InputStream *in);
//	bool unexpectedEOF(int c);
	bool checkContinuationByte(int c);
	bool startOf1ByteSequence(int leadByte);
	bool startOf2ByteSequence(int leadByte);
	int decode2ByteSequence(int leadByte, InputStream *in);
	bool startOf3ByteSequence(int leadByte);
	int decode3ByteSequence(int leadByte, InputStream *in);
	bool startOf4ByteSequence(int leadByte);
	int decode4ByteSequence(int leadByte, InputStream *in);
	bool requires1ByteSequence(wchar_t c);
	bool requires2ByteSequence(wchar_t c);
	bool encode2ByteSequence(wchar_t c, OutputStream *out);
	bool requires3ByteSequence(wchar_t c);
	bool encode3ByteSequence(wchar_t c, OutputStream *out);
	bool requires4ByteSequence(wchar_t c);
	bool encode4ByteSequence(wchar_t c, OutputStream *out);

};

#endif /* UTF8TEXTCODER_H_ */

